import java.util.*;
class Solution {
    //全排列
//    List<List<Integer>> ret;
//    List<Integer> path;
//    boolean[] check;
//    public List<List<Integer>> permute(int[] nums) {
//        ret = new ArrayList<>();
//        path = new ArrayList<>();
//        check = new boolean[nums.length];
//        dfs(nums);
//        return ret;
//    }
//    public void dfs(int[] nums){
//        if(nums.length == path.size()){
//            ret.add(new ArrayList(path));
//            return;
//        }
//        for(int i = 0;i < nums.length;i++){
//            if(check[i] == false){
//                path.add(nums[i]);
//                check[i] = true;
//                dfs(nums);
//                check[i] = false;
//                path.remove(path.size()-1);
//            }
//        }
//    }
    //N皇后
/*    public boolean[] col,kx1,kx2;
    public char[][] path;
    public int n;
    List<List<String>> ret;
    public List<List<String>> solveNQueens(int _n) {
        n = _n;
        col = new boolean[n];
        kx1 = new boolean[2 * n];
        kx2 = new boolean[2 * n];
        ret = new ArrayList();
        path = new char[n][n];
        for (int i = 0; i < n; i++)
            Arrays.fill(path[i], '.');
        dfs(0);
        return ret;
    }

    public void dfs(int row) {
        if (n == row) {
            List<String> tmp = new ArrayList();
            for (int i = 0; i < n; i++)
                tmp.add(new String(path[i]));
            ret.add(new ArrayList(tmp));
        }
        for (int i = 0; i < n; i++) {
            if (col[i] == false && kx1[row - i + n] == false && kx2[row + i] == false) {
                path[row][i] = 'Q';
                col[i] = kx1[row - i + n] = kx2[row + i] = true;
                dfs(row + 1);
                path[row][i] = '.';
                col[i] = kx1[row - i + n] = kx2[row + i] = false;
            }
        }
    }*/
    //单词搜索
    boolean[][] vis;
    int[] dx = { 1, -1, 0, 0 };
    int[] dy = { 0, 0, 1, -1 };
    int m, n;
    char[] word;
    public boolean exist(char[][] board, String _word) {
        m = board.length;
        n = board[0].length;
        word = _word.toCharArray();
        vis = new boolean[m][n];
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++) {
                 if (board[i][j] != word[0])
                     continue;
                 vis[i][j] = true;
                 if (dfs(board, i, j, 1))
                     return true;
                 vis[i][j] = false;
/*                if (board[i][j] == word[0]) {
                    vis[i][j] = true;
                    if (dfs(board, i, j, 1))
                        return true;
                    vis[i][j] = false;
                }*/
            }
        return false;
    }
    public boolean dfs(char[][] board, int i, int j, int pos) {
        if (pos == word.length)
            return true;
        for (int k = 0; k < 4; k++) {
            int x = i + dx[k], y = j + dy[k];
            if (x < m && x >= 0 && y < n && y >= 0 && !vis[x][y] && board[x][y] == word[pos]) {
                vis[x][y] = true;
                if (dfs(board, x, y, pos + 1))
                    return true;
                vis[x][y] = false;
            }
        }
        return false;
    }
}